package com.shawn.admin.controller;

import com.shawn.admin.exception.BusinessException;
import com.shawn.admin.service.ManageService;
import com.shawn.commons.utils.RC;
import com.shawn.model.entity.Log;
import com.shawn.model.entity.UserInfo;
import com.shawn.model.mongo.Video;
import com.shawn.model.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 管理控制器
 *
 * @author lenovo
 * @date 2022/11/07
 */
@Slf4j
@RestController
@RequestMapping("/manage")
public class ManageController {


    @Autowired
    private ManageService manageService;

    @PostMapping("/messages/pass")
    public ResponseEntity passAudit(@RequestBody List<String> ids){
       Map<String,String> map= manageService.passOrNot(ids, RC.MOVEMENT_PASS);
       return ResponseEntity.ok(map);
    }

    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody List<String> ids){
        Map<String,String> map= manageService.passOrNot(ids,RC.MOVEMENT_REJECT);
        return ResponseEntity.ok(map);
    }

    /**
     * 分页获取所有用户信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity getUsersByPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize,
                                        Long id,String nickname,String city ){
      PageResult<UserDetailInfoVo> pageResult = manageService.getUsersByPage(page,pageSize,id,nickname,city);
      return ResponseEntity.ok(pageResult);
    }

    /**
     * 通过id获取用户信息
     *
     * @param userID 用户id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity getUserInfoById(@PathVariable Long userID){
        UserPersonalVo vo = manageService.selectUserById(userID);
        return ResponseEntity.ok("{\"id\":9005554,\"nickname\":\"范娟\",\"mobile\":\"-4662124853\",\"sex\":\"未知\",\"personalSignature\":\"标影低议力\",\"age\":42,\"countBeLiked\":445,\"countLiked\":23,\"countMatching\":104,\"income\":102,\"occupation\":\"华意多才个少\",\"userStatus\":\"1\",\"created\":1598608534811,\"city\":\"泸州市\",\"lastActiveTime\":1598608534811,\"lastLoginLocation\":\"哈尔滨市\",\"logo\":\"https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tanhua/header-03.png\",\"tags\":\"音乐,上班族,麦霸,网瘾少年,手办\"}");
    }

    /**
     * 通过id获取用户信息
     *
     * @param userID 用户id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/logs")
    public ResponseEntity getUserLogById( Long uid,Integer page,Integer pagesize,String sortProp,String sortOrder){
        PageResult<Log> pageResult = manageService.getUserLogById(uid,page,pagesize,sortProp,sortOrder);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 分页获取动态信息
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param uid      uid
     * @param state    状态
     * @return {@link ResponseEntity}
     */
    @GetMapping("/messages")
    public ResponseEntity getMovementsByUid(@RequestParam(defaultValue = "1")Integer page
            ,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize
             ,Long id,Integer state){
         PageResult<MovementDetailVo> pageResult = manageService.getMovByPage(page,pageSize,id,state);

        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/messages/{id}")
    public ResponseEntity getMovementById(@PathVariable String id){
        MovementDetailVo mov = manageService.getMovById(id);
        return ResponseEntity.ok(mov);
    }

    /**
     * 根据动态id查询隶属于该动态的所有评论
     *
     * @param page      页面
     * @param pageSize  页面大小
     * @param messageID 消息id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/messages/comments")
    public ResponseEntity getCommentsByMid(@RequestParam(defaultValue = "1")Integer page
            ,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize,String messageID,
                                           String id,String sortOrder){
       PageResult<CommentBackendVo> pageResult = manageService.getCommentsByMid(page,pageSize,messageID,id,sortOrder);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/videos")
    public ResponseEntity getVideos(@RequestParam(defaultValue = "1")Integer page
            ,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize
            ,Long uid,String sortProp,String sortOrder){
        PageResult<VideoBackendVo> pageResult = manageService.getVideoByPage(page,pageSize,uid,sortProp,sortOrder);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 用户冻结
     *userId 用户id
     * freezingTime 冻结时间 1:三天 2:七天 3:永久冻结
     * freezingRange 冻结范围: 1:冻结登录 2:冻结发言 3:冻结发布动态
     * reasonsForFreezing 冻结原因
     * frozenRemarks 冻结备注
     * @param map 地图
     * @return {@link ResponseEntity}<{@link String}>
     */
    @PostMapping("/users/freeze")
    public ResponseEntity userFreeze(@RequestBody Map<String, Object> map){
        return manageService.userFreeze(map);
    }

    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map<String, Object> map){
        return manageService.unfreeze(map);
    }
}
